package com.xlw.ui.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.MapView;
import com.xlw.onroad.R;
import com.xlw.presenter.IMainView;
import com.xlw.presenter.MainPresenter;

/*
当前坐标:
    经度: 116.226536
    纬度: 39.954314
 */
public class MainActivity extends AppCompatActivity implements IMainView {

    private MainPresenter presenter;    // 主导器 - MVP模式中的P

    private MapView mapView ;
    private AMap aMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mapView = (MapView)findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);

        initMap();
        initPresent();
    }

    /**
     * 初始化AMap对象
     */
    private void initMap() {
        if (aMap == null) {
            aMap = mapView.getMap();
        }
    }

    /**
     * 为当前Activity初始化主导器
     */
    private void initPresent(){
        presenter = new MainPresenter();
        presenter.setView(this);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
        presenter.viewFinishLoading();  // 执行主导器代码
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id){
            case R.id.test_dao:
                presenter.tripDaoTest();    // 由主导器处理dao数据访问,并回调显示结果
                break;
        }
        return true;
    }

    @Override
    public void showDaoTestResult(String resultCode) {
        // 显示dao测试结果
        Toast.makeText(this, resultCode, Toast.LENGTH_LONG).show();
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void showNoInetErrorMsg() {

    }

    @Override
    public void moveToNextActivity() {

    }
}
